package com.cf.spider.core.model.service;

import com.cf.crud.service.AbsBaseService;
import com.cf.id.SnowflakeId;
import com.cf.spider.SpiderSystemProperty;
import com.cf.spider.core.model.EnumJobState;
import com.cf.spider.core.model.JobInfo;
import com.cf.spider.core.model.JobTask;
import com.cf.spider.core.model.dao.JobTaskDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * boot-awm-JobTaskService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/11 上午 10:16
 */
@Slf4j
public class JobTaskService extends AbsBaseService<JobTask, JobTaskDao> {
    @Override
    public Class getTargetClass() {
        return JobTask.class;
    }

    @Autowired
    private SnowflakeId id;

    @Autowired
    private SpiderSystemProperty spiderSystemProperty;

    /**
     * 获取为抓取的任务
     *
     * @return
     */
    public List<JobTask> getAllFetchTask() {
        List<JobTask> allFetchTask = this.mapper.getAllFetchTask(spiderSystemProperty.getMaxFetchNum());
        if (allFetchTask != null && allFetchTask.size() > 0) {
            this.mapper.prepare(allFetchTask);
        }
        return allFetchTask;
    }

    public void end(JobTask jobTask) {
        jobTask = selectOne(jobTask.getId());
        jobTask.setState(EnumJobState.END.getState());
        jobTask.setLastTryTime(new Date());
        this.mapper.updateByPrimaryKey(jobTask);
    }

    public void success(JobTask jobTask, String data) {
        jobTask = selectOne(jobTask.getId());
        jobTask.setState(EnumJobState.SUCCESS.getState());
        jobTask.setData(data);
        jobTask.setFetchRetry(jobTask.getFetchRetry() + 1);
        jobTask.setLastTryTime(new Date());
        this.mapper.updateByPrimaryKey(jobTask);
    }

    public void error(JobTask jobTask, String message) {
        jobTask = selectOne(jobTask.getId());
        jobTask.setState(EnumJobState.ERROR.getState());
        jobTask.setMsg(message);
        jobTask.setFetchRetry(jobTask.getFetchRetry() + 1);
        jobTask.setLastTryTime(new Date());
        this.mapper.updateByPrimaryKey(jobTask);
    }

    public void addTask(JobInfo data, String urls) {
        if (StringUtils.isEmpty(urls)) {
            log.info("不存在下载地址 【{}】。。。。", urls);
            return;
        }
        long jobId = data.getId();
        String[] split = urls.split(",");
        if (split == null || split.length == 0) {
            log.info("无需要下载的地址 【{}】。。。。", urls);
            return;
        }
        List<JobTask> jobTasks = new ArrayList<>(split.length);
        for (String s : split) {
            JobTask jobTask = new JobTask();
            jobTask.setJobId(jobId);
            jobTask.setState(EnumJobState.PREPARE.getState());
            jobTask.setUrl(s);
            jobTask.setId(id.nextId());
            jobTasks.add(jobTask);
        }
        this.mapper.insertListWithId(jobTasks);
    }

    /**
     * 获取需要推送的任务
     *
     * @return
     */
    public List<JobTask> getAllPushTask() {
        List<JobTask> allFetchTask = this.mapper.getAllPushTask(spiderSystemProperty.getMaxPushNum());
        if (allFetchTask != null && allFetchTask.size() > 0) {
            this.mapper.retryPush(allFetchTask);
        }
        return allFetchTask;
    }
}
